<?php
namespace app\index\controller;
use app\index\controller\Api as Api;
use think\Db;
use think\Cookie;
use think\Controller;
class Index extends Controller
{
    function hidtel($phone){
        $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话
        if($IsWhat == 1){
            return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);
        }else{
            return  preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
        }
    }
    /**
     * 首页 行情列表
     * @author lukui  2017-02-18
     * @return [type] [description]
     */
    public function index()
    {
        //获取产品信息
        $api= new Api();
        $api->getdate();
        //获取产品信息
        $pro = Db::name('productinfo')->alias('pi')->field('pi.pid,pi.ptitle,pi.img,pd.Price,pd.UpdateTime,pd.Low,pd.High,pd.Open')
            ->join('__PRODUCTDATA__ pd','pd.pid=pi.pid')
            ->where('pi.isdelete',0)->order('pi.ordernum desc,pi.pid desc')->select();

        $paihang_list[0]['mobile']  = '13' . substr(uniqid('', true), 19) . substr(microtime(), 2, 5);;
        $paihang_list[1]['mobile']  = '13' . substr(uniqid('', true), 19) . substr(microtime(), 2, 5);;
        $paihang_list[0]['num'] = mt_rand(1000,99999);
        $paihang_list[1]['num'] = mt_rand(1000,99999);
        // 取得列的列表
        foreach ($paihang_list as $key => $row)
        {
            $volume[$key]  = $row['num'];
            $edition[$key] = $row['mobile'];
        }

        array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $paihang_list);
        foreach ($paihang_list as $key=>$val){
            $paihang_list[$key]['mobile'] =$this->hidtel($val['mobile']);
        }

        $this->assign('paihang_list',$paihang_list);
        $this->assign('pro',$pro);


        return $this->fetch();
    }

    public function shengou()
    {
        //获取产品信息
        $pro = Db::name('productinfo')->alias('pi')->field('pi.pid,pi.ptitle,pd.Price,pd.UpdateTime,pd.Low,pd.High')
            ->join('__PRODUCTDATA__ pd','pd.pid=pi.pid')
            ->where('pi.isdelete',0)->order('pi.pid desc')->select();
        //dump(cookie('pid7'));
        $this->assign('pro',$pro);
        return $this->fetch();
    }

    public function index22()
    {
        //获取产品信息
        $pro = Db::name('productinfo')->alias('pi')->field('pi.pid,pi.ptitle,pd.Price,pd.UpdateTime,pd.Low,pd.High')
            ->join('__PRODUCTDATA__ pd','pd.pid=pi.pid')
            ->where('pi.isdelete',0)->order('pi.pid desc')->select();
        //dump(cookie('pid7'));
        $this->assign('pro',$pro);
        return $this->fetch();
    }

    public function ajaxindexpro()
    {
        //获取产品信息
        $api= new Api();
        $api->getdate();

        // dump($api);
        //获取产品信息
        $pro = Db::name('productinfo')->alias('pi')->field('pi.pid,pi.ptitle,pd.Price,pd.UpdateTime,pd.Low,pd.High')
            ->join('__PRODUCTDATA__ pd','pd.pid=pi.pid')
            ->where('pi.isdelete',0)->order('pi.pid desc')->select();
        $newpro = array();
        foreach ($pro as $k => $v) {
            $newpro[$v['pid']] = $pro[$k];
            $newpro[$v['pid']]['UpdateTime'] = date('H:i:s',$v['UpdateTime']);


            // if(isset($_COOKIE['pid'.$v['pid']])){
            //     cookie('pid'.$v['pid'],566855);
            //     continue;
            // }
            if($v['Price'] < cookie('pid'.$v['pid']) ){  //跌了
                $newpro[$v['pid']]['isup'] = 0;
            }elseif($v['Price'] > cookie('pid'.$v['pid']) ){  //涨了
                $newpro[$v['pid']]['isup'] = 1;
            }else{  //没跌没涨
                $newpro[$v['pid']]['isup'] = 2;
            }
            cookie('pid'.$v['pid'],$v['Price']);
        }
        return $newpro;
    }

}
